<?php
/*
 * Created on 2010-5-31
 * 
 * Filename: register.php
 *
 * Author: szy
 * 
 * TODO: TODO
 *
 */
  //include function files for this application
  require_once('funs/bbox_fns.php');
  require_once('main.php');
  
  //create short variable names
  $email = $_POST['email'];
  $passwd = $_POST['passwd'];
  $passwd2 = $_POST['passwd2'];
  
  //start session which may be needed later
  //start it now because it must go before headers
  session_start();
  
  try{
  
    //check forms filled in
    if(!filled_out($_POST)){
    
      throw new Exception('You have not filled the form out correctly. Please go back and try again.');	
    	
    }  //end if	
    
    //email address not valid
    if(!valid_email($email)){
    
      throw new Exception('That is not a valid email address. Please go back and try again.');	
    	
    }  //end if
    
    //passwords not the same
    if($passwd != $passwd2){
    
      throw new Exception('The passwords you entered do not match. Please go back and try again.');	
    	
    }  //end if
  	
  	//check password length is ok
  	if(strlen($passwd)<6){
  	
  	  throw new Exception('Your password must be at least 6 characters long. Please go back and try again.');	
  		
  	}  //end if
  	
  	//attempt to register
  	//this function can also throw an exception
  	register($email, $passwd);
  	
  	//register session variable
  	$_SESSION['valid_user'] = $email;

  	$tpl->assign('sessionid', $_SESSION['valid_user']);
  	$tpl->display('register.html');
  
  }
  catch(Exception $e){
  
    $tpl->assign('error', $e->getMessage());
    $tpl->assign('msg', 'Your register could not be done. Please try again later.');
    $tpl->assign('back_link', 'index.php');
    $tpl->display("error.html");
    exit;
  	
  }


?>
